//
//  ViewController.m
//  Client
//
//  Created by jyh on 2018/7/25.
//  Copyright © 2018年 huig. All rights reserved.
//

#import "ViewController.h"
#import "ClientManager.h"
#import "Immessage.pbobjc.h"
#import "MessageCompile.h"
#import "MessageModel.h"

@interface ViewController ()<ClientManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *userId;
@property (weak, nonatomic) IBOutlet UITextView *showMessageView;
@property (weak, nonatomic) IBOutlet UITextView *sendMessageView;
@property (weak, nonatomic) IBOutlet UITextField *address;
@property (weak, nonatomic) IBOutlet UITextField *post;
@property (weak, nonatomic) IBOutlet UITextField *receiverText;
@property (nonatomic, strong) messageInfo *msgInfo;
@property (nonatomic, strong) UserInfo *user;
@property (nonatomic, strong) TextMessage *tm;
@property (nonatomic, strong) GiftMessage *gm;
@property (nonatomic, strong) ClientManager *clientManger;
@end

@implementation ViewController

- (messageInfo *)msgInfo {
    if (!_msgInfo) {
        _msgInfo = [messageInfo new];
    }
    return _msgInfo;
}

- (UserInfo *)user{
    if (!_user) {
        _user = [UserInfo new];
    }
    return _user;
}

- (TextMessage *)tm {
    if (!_tm) {
        _tm = [TextMessage new];
        self.msgInfo.sender = self.user.name;
        self.msgInfo.receiver = @"";
        _tm.msgInfo = self.msgInfo;
        _tm.user = self.user;
    }
    return _tm;
}

- (GiftMessage *)gm {
    if (!_gm) {
        _gm = [GiftMessage new];
        self.msgInfo.sender = self.user.name;
        self.msgInfo.receiver = @"";
        _gm.msgInfo = self.msgInfo;
        _gm.user = self.user;
    }
    return _gm;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.user.name = [NSString stringWithFormat:@"huig_%u",arc4random_uniform(100)];
    self.user.level = arc4random_uniform(100);
    self.userId.text = self.user.name;
}
// 进入房间
- (void)joinRoom {
    self.tm.text = @"进入房间";
    NSData *data = [MessageCompile packageMessage:self.tm messageType:(messageTypeJoinRoom) packageType:(packageTypeText)];
    [self.clientManger sendMessage:data];
}
// 离开房间
- (void)leaveRoom {
    self.tm.text = @"离开房间";
    NSData *data = [MessageCompile packageMessage:self.tm messageType:(messageTypeLeaveRoom) packageType:(packageTypeText)];
    [self.clientManger sendMessage:data];
}
// 文本信息TextMessage
- (void)chatMsg {
    self.tm.text = @"chatMsg";
    NSData *data = [MessageCompile packageMessage:self.tm messageType:(messageTypeChat) packageType:(packageTypeText)];
    [self.clientManger sendMessage:data];
}
// 礼物信息GiftMessage
- (void)giftMsg {
    self.gm.giftName = @"轰炸机";
    self.gm.giftURL = @"http://www.baidu.com";
    self.gm.giftCount = 10;
    self.gm.msgInfo.receiver = @"_";
    NSLog(@"%@",self.gm.msgInfo);
    NSData *data = [MessageCompile packageMessage:self.gm messageType:(messageTypeGift) packageType:(packageTypeText)];
    [self.clientManger sendMessage:data];
}

// 测试发送几种信息
- (IBAction)LKFJClick:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            [self leaveRoom];
            break;
        case 1:
            [self chatMsg];
            break;
        case 2:
            [self giftMsg];
            break;
    }
}

#pragma mark -- ClientManagerDelegate

/** 接收到信息调用 */
- (void)clientManager:(ClientManager *)client data:(NSData *)data {
    [self messageMake:data];
}

#pragma mark -- 内部方法

// 处理接收到的信息用于展示
- (void)messageMake:(NSData *)msgData {
    MessageModel *msgM = [MessageCompile parsingWithData:msgData];
    /*
    type = 0,心跳包;1,系统信息;2,进入房间;3,离开房间;4,文本信息;5,礼物信息
     */
    if (msgM.type == 0 || msgM.type > 6) return;

    if (msgM.type == 1) {
        NSString *str = msgM.msgData;
        [self showMessage:str];
        return;
    }
    
    if (msgM.type < 6 && msgM.type > 1) {
        TextMessage *tm = (TextMessage*)msgM.msgData;
        NSString *str = [NSString stringWithFormat:@"%lld:%@,%@",tm.user.level,tm.user.name,tm.text];
        [self showMessage:str];
        return ;
    }
    
    if (msgM.type == 6) {
        GiftMessage *gm = (GiftMessage*)msgM.msgData;
        NSString *str = [NSString stringWithFormat:@"%lld:%@,%@,%@,%lld",gm.user.level,gm.user.name,gm.giftName,gm.giftURL,gm.giftCount];
        NSLog(@"%@",str);
        [self showMessage:str];
        return;
    }
}

// 连接到服务器
- (IBAction)connectServerClick:(UIButton *)sender {
    BOOL isSuccess = [self.clientManger connectServerAddress:self.address.text post:self.post.text.intValue];
    if (isSuccess) {
        [self joinRoom];
    } else {
        [self showMessage:@"连接失败"];
    }
}

// 发送信息
- (IBAction)sendMessageClick:(UIButton *)sender {
    
    // 定向信息
    if (self.receiverText.text.length > 0) {
        self.tm.msgInfo.receiver = self.receiverText.text;
        self.tm.text = self.sendMessageView.text;
        [self.clientManger sendMessage:[MessageCompile packageMessage:self.tm messageType:(messageTypeReceiver) packageType:(packageTypeText)]];
        return;
    }
    
    // 群聊信息
    self.tm.text = self.sendMessageView.text;
    [self.clientManger sendMessage:[MessageCompile packageMessage:self.tm messageType:(messageTypeChat) packageType:(packageTypeText)]];
    self.sendMessageView.text = nil;
}

// 显示信息
- (void)showMessage:(NSString *)messageStr {
    self.showMessageView.text = [self.showMessageView.text stringByAppendingFormat:@"%@\n",messageStr];
    [self.showMessageView scrollRangeToVisible:NSMakeRange(self.showMessageView.text.length, 1)];
    self.showMessageView.layoutManager.allowsNonContiguousLayout = NO;
}

- (ClientManager *)clientManger {
    if (!_clientManger) {
        _clientManger = [ClientManager shareInstance];
        _clientManger.delegate = self;
    }
    return _clientManger;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
